<?php
include_once("../../php/include_all.php");
session_start();
$act = '';
if (isset($_POST['act']))
    $act = $_POST['act'];

if ($act == 'tmpl_program') {
    $idkec = $_POST['idSk'];
    if (is_numeric($idkec)) {
        echo 'ok___';
        view_data($idkec);
    } else {
        echo "er";
    }
} else if ($act == 'rincProg') {
    //$bagian = htmlentities($_POST['bgian']);
    $id = $_POST['id'];
//    echo $bagian." => ".$id;
    view_tbelRinc($id);
} else if ($act == 's_rincian') {
    //$kode = htmlentities($_POST['kde']);
    $idUs = $_POST['id_us'];
    $status = $_POST['status'];
    $apbdkab = $_POST['apbdkab'];
    $apbdprov = $_POST['apbdprov'];
    $apbn = $_POST['apbn'];
    $ket = $_POST['keterangan'];
    $prioritas = $_POST['prioritas'];
	$rows = $_POST['rows'];
    /*for ($i=0; $i<count($rows); $i++) {
		exec_query("UPDATE mus_rinc_usulan_kec SET status =" . $status[$i] . ",apbdkab = ".$apbdkab[$i].",apbdprov = ".$apbdprov[$i].", apbn = ".$apbn[$i].", prioritas = ".$prioritas[$i].", ket = '".$ket[$i]."' WHERE id_rinc_usulan_kec = " . $idUs[$i]);
		echo 'success__';*/
         if (count($idUs) > 0){
            foreach ($idUs as $key => $value) {
                $stt = ($status[$key] == '0') ? 'NULL' : $status[$key];
                $apKab = ($apbdkab[$key] == '0' || empty($apbdkab[$key])) ? '0' : $apbdkab[$key];
                $apProv = ($apbdprov[$key] == '0' || empty($apbdprov[$key])) ? '0' : $apbdprov[$key];
                $apN = ($apbn[$key] == '0' || empty($apbn[$key])) ? '0' : $apbn[$key];
                $ketRangan = str_replace("'", "''", $ket[$key]);
                $prio = ($prioritas[$key] == '0') ? 'NULL' : $prioritas[$key];              
                $ff = exec_query("UPDATE mus_rinc_usulan_kec SET status =" . $stt . ",apbdkab = $apKab,apbdprov = $apProv, apbn = $apN, prioritas = $prio, ket = '$ketRangan' WHERE id_rinc_usulan_kec = " . $value);
                if (!$ff) {
                    $rslt = '2';
                    $er = error_get_last();
                    $error = 'Error in : ';
                    foreach ($er as $value) {
                        $error.= ($key == 0) ? 'Error in : ' . $value : $value;
                    }
                    break 1;
                } else {
                    $rslt = '1';
                }
				
				logs('Memverifikasi usulan SKPD dengan status '.$stt.' dengan keterangan '.$ketRangan,'ID usulan '.$value);
            }
            $rst = (!isset($rslt)) ? '1' : $rslt;
            echo $rslt . "__" . $error;
    } else {
        echo '0__';
    }
	
}

function view_data($idkec) {
    $year = date('Y');
	$cek = get_datas ("select distinct u.id_usulankec from mus_usulankecamatan u, mus_rinc_usulan_kec r, mus_kecamatan c, mus_skpd s where s.id_skpd=u.id_skpd and r.id_usulankec=u.id_usulankec and u.id_kecamatan=c.id_kecamatan and u.id_kecamatan=$idkec and r.status=0 and u.tahun=$year and u.id_skpd=".musrenbang_idskpd);
    $data = get_datas("select distinct p.id_program, p.nama_program from mus_program p, mus_kecamatan c, mus_kegiatan k, mus_usulankecamatan u, mus_rinc_usulan_kec r, mus_skpd s where u.id_skpd=s.id_skpd and u.id_kecamatan=c.id_kecamatan and u.id_usulankec=r.id_usulankec and u.id_kegiatan=k.id_keg and k.id_program=p.id_program and u.id_kecamatan=$idkec and r.status=0 and u.tahun=$year and u.id_skpd=".musrenbang_idskpd);
    $keg = "";
    $pro1 = ""; //<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
    $no = 1;
    if (count($cek) > 0) {
        foreach ($data as $dataProgram) {
            ?>        
            <tr><td><?php echo $no; ?></td><td colspan="4"><?php echo ucfirst($dataProgram['nama_program']); ?></td></tr>
            <?php
            $dataudesa = get_datas("select distinct u.id_usulankec, c.id_kecamatan, c.nama_kecamatan, s.id_skpd, s.nama_skpd, k.nama_kegiatan, u.indikator_hasil, u.indikator_program, u.indikator_keluaran, u.target_hasil 
									from mus_usulankecamatan u, mus_rinc_usulan_kec r, mus_kecamatan c, mus_kegiatan k, mus_program p, mus_skpd s 
									where s.id_skpd=u.id_skpd and r.id_usulankec=u.id_usulankec 
									and u.id_kecamatan=c.id_kecamatan and u.id_kecamatan=$idkec
									and r.status=0 and k.id_keg=u.id_kegiatan 
									and u.tahun=$year
									and k.id_program=p.id_program 
									and k.id_program = ".$dataProgram['id_program']."
									and u.id_skpd = ".musrenbang_idskpd."
									order by u.id_usulankec");
           
                foreach ($dataudesa as $value) {
                    ?>
                    <tr><td>&nbsp;</td><td style="padding-left: 25px;"><a href='#windowRincPro' data-toggle="modal" onclick="showRincProg(this)" name="<?php echo $value['id_usulankec']; ?>"><?php echo ucfirst($value['nama_kegiatan']); ?></a></td>
                        <td><?php echo $value['indikator_program']; ?></td>
                        <td><?php echo $value['indikator_keluaran']; ?></td>
                        <td><?php echo $value['indikator_hasil']; ?></td>
                        <td><?php echo $value['target_hasil']; ?></td>
                    </tr> 
                    <?php
                }
            
            $no++;
        }
		$tampil = get_datas ("select distinct u.id_usulankec, c.id_kecamatan, c.nama_kecamatan, u.indikator_hasil, u.indikator_program, u.indikator_keluaran, u.target_hasil, u.kegiatan from mus_usulankecamatan u, mus_rinc_usulan_kec r, mus_kecamatan c, mus_skpd s where s.id_skpd=u.id_skpd and c.id_kecamatan=u.id_kecamatan and r.id_usulankec=u.id_usulankec and u.id_kecamatan=$idkec and r.status=0  and u.tahun=$year and u.id_skpd=".musrenbang_idskpd." and u.id_kegiatan=0 order by u.id_usulankec");
		foreach ($tampil as $tampil){
			?>
            <tr>
            	<td><?php echo $no; ?></td>
            	<td style="padding-left: 25px;"><a href='#windowRincPro' data-toggle="modal" onclick="showRincProg(this)" name="<?php echo $tampil['id_usulankec']; ?>"><?php echo $tampil['kegiatan']; ?></td>
            	<td><?php echo $tampil['indikator_program']; ?></td>
            	<td><?php echo $tampil['indikator_keluaran']; ?></td>
                <td><?php echo $tampil['indikator_hasil']; ?></td>
            	<td><?php echo $tampil['target_hasil']; ?></td>
            </tr>
            <?php $no++;
		}
    }
	else {
        echo "<tr><td colspan='5'><div class='alert center'>Tidak ada data</div></td></tr>";
    }
}

function view_tbelRinc($id) {
    $status = array('Diterima', 'Ditolak', 'Dipertimbangkan');
    $iconSt = array(1 => 'icon-terima', 2 => 'icon-tolak', 3 => 'icon-pertimbangan');

    
            ?>
            
            <input type='hidden' name='act' value='s_rincian' />
            <table class="table table-bordered table-striped geo-table">
                <tr><th> No</th><th>Lokasi</th><th>Volume</th><th>APBD Kab</th><th>APBD Prov</th><th>APBN</th><th>Status</th><th>Prioritas</th><th>Keterangan</th></tr>
                <?php
                $dt = get_datas("SELECT id_rinc_usulan_kec, lokasi, volume, apbdkab, apbdprov, apbn,status,prioritas,ket FROM mus_rinc_usulan_kec where id_usulankec = " . $id);
                $no = 1;
				$counter =0;
                $jumData = count($dt);
                foreach ($dt as $value) {
                    $lks = (empty($value['lokasi'])) ? '&nbsp;' : $value['lokasi'];
                    $vlm = (empty($value['volume'])) ? '&nbsp;' : $value['volume'];
                    ?>
                    <tr><td><?php echo $no; ?></td>                
                        <td style='vertical-align:middle;'><?php echo $lks; ?><input type='hidden' name='id_us[]' value='<?php echo $value['id_rinc_usulan_kec']; ?>' />
                        <input type='hidden' name='rows[]' value='<?php echo $counter++ ; ?>'></td>
                        <td style='vertical-align:middle;'><?php echo $vlm; ?></td>
                        <td style='vertical-align:middle;text-align: right;'><input type='text' name='apbdkab[]' value='<?php echo $value['apbdkab']; ?>' /></td>
                        <td style='vertical-align:middle;text-align: right;'><input type='text' name='apbdprov[]' value='<?php echo $value['apbdprov']; ?>' /></td>
                        <td style='vertical-align:middle;text-align: right;'><input type='text' name='apbn[]' value='<?php echo $value['apbn']; ?>' /></td>
                        <td style='vertical-align:middle;'><span id='icn_<?php echo $no; ?>' class='<?php if (!empty($value['status'])) echo $iconSt[$value['status']]; ?>'></span><select id='<?php echo $no; ?>' class='status span2' name="status[]"><option value=0>--Pilih status--</option><?php
                                foreach ($status as $key => $isi) {
                                    $st = ($value['status'] == ($key + 1)) ? 'selected' : '';
                                    echo "<option value=" . ($key + 1) . " " . $st[$key] . ">" . $isi . "</option>";
                                }
                                ?></select></td>
                        <td style='vertical-align:middle;text-align: right;'><select name='prioritas[]' class='span1'><option value=0>--Pilih Prioritas--</option><?php
                                $i = 1;
                                while ($i <= count($dt)) {
                                    $slc = ($i == $value['prioritas']) ? 'seleceted' : '';
                                    echo "<option value=$i $slc>$i</option>";
                                    $i++;
                                }
                                echo $value['prioritas'];
                                ?></select></td>
                         <td><textarea name='keterangan[]' ><?php echo $value['ket']; ?></textarea></td>
                    </tr>
                    <?php
                    $no++;
                }
            
            ?></table>
        <div class="pull-right">
            <span id='loader' style='width:30px;height:30px;'></span><span id='result'></span><button type="button" class="btn btn-primary btn-simpan-usulan"><i class="icon-white icon-check"></i>&nbsp;Simpan</button>
        </div>   
        <?php
    
}
?>